package com.service.component.shiro;

import com.service.component.common.CommonResult;
import com.service.component.permissions.entity.Permission;
import com.service.component.permissions.services.PermissionService;
import com.service.component.relation.services.RelationServices;
import com.service.component.role.entity.Role;
import com.service.component.role.services.RoleServices;
import com.service.component.user.entity.Users;
import com.service.component.user.services.UserServices;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author ：Calvin
 * @date ：Created in 2021/7/16 14:35
 * @description：自定义Realm
 * @modified By：
 * @version: 1.0.0$
 */
@Slf4j
@Component
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private UserServices userServices;

    @Autowired
    private RelationServices relationServices;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        log.info("-------授权--------");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        // 根据当前登录用户 查询对应角色和权限
        Users user = (Users) principalCollection.getPrimaryPrincipal();
        // 调用业务层，查询角色
        List<Role> roles = relationServices.getUserRolesByUserId(user.getId());
        for (Role role : roles) {
            authorizationInfo.addRole(role.getRoleNameEN());
        }
        // 调用业务层，查询权限
        List<Permission> permissions = relationServices.getUserPermissionByUserId(user.getId());
        for (Permission permission : permissions) {
            authorizationInfo.addStringPermission(permission.getPermNameEN());
        }
        return authorizationInfo;
    }

    /**
     * 获取即将需要认证的信息
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.info("-------身份认证--------");
        String username = (String) authenticationToken.getPrincipal();
        String password = new String((char[]) authenticationToken.getCredentials());
        Users user = userServices.getUserByUsername(username);
        //根据用户名从数据库获取密码
        if (ObjectUtils.isEmpty(user)) {
            throw new AccountException(CommonResult.ResultCode.DATA_NOT_EXIST.getMessage());
        }
        return new SimpleAuthenticationInfo(user,user.getPassword(),ByteSource.Util.bytes(user.getSalt()),this.getName());
    }
}
